package com.dev.receiver;

import com.dev.utils.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;

/**
 * 监听应用变化的订阅公告事件类
 */
public class PackageSubscriber extends CommonEventSubscriber {

    private AbilitySlice slice;
    public static final String COMMON_EVENT_PACKAGE_REPLACED = "usual.event.PACKAGE_REPLACED";
    public static final String COMMON_EVENT_PACKAGE_REMOVED = "usual.event.PACKAGE_REMOVED";
    public static final String COMMON_EVENT_PACKAGE_FULLY_REMOVED = "usual.event.PACKAGE_FULLY_REMOVED";
    public static final String COMMON_EVENT_PACKAGE_ADDED = "usual.event.PACKAGE_ADDED";

    public PackageSubscriber(CommonEventSubscribeInfo subscribeInfo, AbilitySlice slice) {
        super(subscribeInfo);
        this.slice = slice;
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        if (commonEventData == null || commonEventData.getIntent() == null) {
            return;
        }
        if (COMMON_EVENT_PACKAGE_REPLACED.equals(commonEventData.getIntent().getAction())) {
            new ToastDialog(slice).setText("应用更新！").show();
            LogUtils.info("PACKAGE", "package is update!");
        } else if (COMMON_EVENT_PACKAGE_REMOVED.equals(commonEventData.getIntent().getAction())) {
            new ToastDialog(slice).setText("应用卸载！").show();
            LogUtils.info("PACKAGE", "package is removed!");
        } else if (COMMON_EVENT_PACKAGE_ADDED.equals(commonEventData.getIntent().getAction())) {
            new ToastDialog(slice).setText("应用安装！").show();
            LogUtils.info("PACKAGE", "battery is okay!");
        }
    }
}
